<?php
$user = new User();
$islogin = 0;
$u_token = $_COOKIE['u_token'];
$admin_token = $_COOKIE['admin_token'];

if ($u_token){
    $user_info = json_decode($user->islogin($u_token),true);

    if(isset($_GET['sudo'])){   //提权
        if($user_info['message']['u_type']==1){
            setcookie("admin_token", $u_token, time() + (86400 * COOKIE_OVERDUE_DAY), "/");
            header('location: /admin/');
            exit;
        }
    }

    $res_status = $user_info['status'];
    if($res_status==200){
        $islogin = 1;
    }else{
        setcookie("u_token", "", time() - 3600, "/");
        header("Location:index.php");
        exit;
    }
}


if ($admin_token && strpos($_SERVER['REQUEST_URI'], '/admin/') !== false){
    $user_info = json_decode($user->islogin($admin_token),true);
    $res_status = $user_info['status'];
    if($res_status==200){
        if($user_info['message']['u_type']==1){
            $isadmin = 1;
        }
    }
}

if(isset($_GET['logout'])){
    setcookie("u_token", "", time() - 3600, "/");
    setcookie("admin_token", "", time() - 3600, "/");
    header('location: /');
    exit;
}

if(isset($_GET['recovtk'])){
    setcookie("u_token", $admin_token, time() + (86400 * COOKIE_OVERDUE_DAY), "/");
    header('location: /admin/');
    exit;
}
